/**
 * 
 */
package com.shine.icis.blo.imp;

import java.util.Date;

import com.shine.cotrun.exp.Export;
import com.shine.cotrun.exp.ExportConf;
import com.shine.cotrun.exp.ExportFactory;
import com.shine.cotrun.sql.QueryParameters;
import com.shine.cotrun.web.comm.ListPage;
import com.shine.icis.blo.IRptA13Service;
import com.shine.icis.dao.IRptA13DAO;
import com.shine.icis.dao.ISubmitConfDAO;
import com.shine.icis.dto.RptA13;
import com.shine.icis.dto.RptA13.RptA13Id;
import com.shine.icis.exception.ICISException;
import com.shine.icis.hbean.RptA13Bak;

/**
 * @author zhangxr
 *
 */
public class RptA13Service implements IRptA13Service {
	
	private IRptA13DAO rptA13DAO;
	private ISubmitConfDAO submitConfDAO;

	public void setRptA13DAO(IRptA13DAO rptA13DAO) {
		this.rptA13DAO = rptA13DAO;
	}
	
	public void setSubmitConfDAO(ISubmitConfDAO submitConfDAO) {
		this.submitConfDAO = submitConfDAO;
	}
	
	@Override
	public ListPage<RptA13> getRptA13Page(QueryParameters qp) {
		if (qp == null) {
			throw new IllegalArgumentException();
		}
		return this.rptA13DAO.findListPage(qp);
	}
	
	@Override
	public ListPage<RptA13Bak> getRptA13BakPage(QueryParameters qp) {
		if (qp == null) {
			throw new IllegalArgumentException();
		}
		return this.rptA13DAO.findBakListPage(qp);
	}

	@Override
	public String csExpDataRptA13(QueryParameters qp, String dir, String expType) {
		if (qp == null) {
			throw new IllegalArgumentException();
		}
		//初始化配置信息
		ExportConf cfg = new ExportConf();
		cfg.setExpToDir(dir);
		cfg.setExpFileName("报送数据调整结果");
		cfg.addField("应收应付类型", "arapTypeText", String.class, 40);
		cfg.addField("应收付原因", "arapReasonText", String.class, 20);
		cfg.addField("交易日期", "formatTradeDate", String.class, 10);
		cfg.addField("币种", "curTypeSipfText", String.class, 15);
		cfg.addField("对方机构", "oppParText", String.class, 20);
		cfg.addField("市场代码", "mktCodeSipfText", String.class, 25);
		cfg.addField("业务类型", "arapBusiTypeText", String.class, 30);
		cfg.addField("应收应付金额", "arapAmt", Double.class);
		cfg.addField("已收付日期", "formatHarapDate", String.class, 10);
		cfg.addField("应收付日期", "formatArapDate", String.class, 10);
		cfg.addField("摘要信息", "summary", String.class, 120);
		Export<RptA13> exp = ExportFactory.createInstance(expType, cfg);
		
		try {
			exp.init();//初始化工作
			
			//添加额处查询条件
			qp.setPageNo(1);
			qp.setPageSize(10000);
			
			ListPage<RptA13> page = this.rptA13DAO.findListPage(qp);//查第一页
			//第一页写到文件
			exp.export(page.getDataList());
			if (page.getTotalPageCount() > 1) {
				for (int i = 2; i <= page.getTotalPageCount(); i++) {
					qp.setPageNo(i);
					page = this.rptA13DAO.findListPage(qp);//查第i页
					//第i页写到文件
					exp.export(page.getDataList());
				}
			}
		} catch(Exception e) {
			e.printStackTrace();
		} finally {
			//结束写入
			exp.end();
		}
		return exp.getFileName();//返回文件名（含扩展名）
	}
	
	@Override
	public String csExpDataRptA13Bak(QueryParameters qp, String dir, String expType) {
		if (qp == null) {
			throw new IllegalArgumentException();
		}
		//初始化配置信息
		ExportConf cfg = new ExportConf();
		cfg.setExpToDir(dir);
		cfg.setExpFileName("报送数据调整日志");
		cfg.addField("操作时间", "oprDateTime", Date.class, 18);
		cfg.addField("操作类型", "opTypeText", String.class, 8);
		cfg.addField("应收应付类型", "arapTypeText", String.class, 40);
		cfg.addField("应收付原因", "arapReasonText", String.class, 20);
		cfg.addField("交易日期", "formatTradeDate", String.class, 10);
		cfg.addField("币种", "curTypeSipfText", String.class, 15);
		cfg.addField("对方机构", "oppParText", String.class, 20);
		cfg.addField("市场代码", "mktCodeSipfText", String.class, 25);
		cfg.addField("业务类型", "arapBusiTypeText", String.class, 30);
		cfg.addField("应收应付金额", "arapAmt", Double.class);
		cfg.addField("已收付日期", "formatHarapDate", String.class, 10);
		cfg.addField("应收付日期", "formatArapDate", String.class, 10);
		cfg.addField("摘要信息", "summary", String.class, 120);
		Export<RptA13Bak> exp = ExportFactory.createInstance(expType, cfg);
		
		try {
			exp.init();//初始化工作
			
			//添加额处查询条件
			qp.setPageNo(1);
			qp.setPageSize(10000);
			
			ListPage<RptA13Bak> page = this.rptA13DAO.findBakListPage(qp);//查第一页
			//第一页写到文件
			exp.export(page.getDataList());
			if (page.getTotalPageCount() > 1) {
				for (int i = 2; i <= page.getTotalPageCount(); i++) {
					qp.setPageNo(i);
					page = this.rptA13DAO.findBakListPage(qp);//查第i页
					//第i页写到文件
					exp.export(page.getDataList());
				}
			}
		} catch(Exception e) {
			e.printStackTrace();
		} finally {
			//结束写入
			exp.end();
		}
		return exp.getFileName();//返回文件名（含扩展名）
	}
	
	/**
	 * 保存A13的信息
	 * @author zhangxr
	 * @param rptA13 A13实体信息
	 * @param userCode 用户编号
	 * */
	@Override
	public void addRptA13(RptA13 rptA13,String userCode) throws ICISException {
		RptA13Id id = rptA13.getId();
		String companyCode = this.submitConfDAO.findCompanyCode(id.getBusiDate());   //获得证券公司编码
		if(companyCode == null ){
			throw new ICISException("证券公司基本信息无记录，不能进行新增");
		}
		id.setCompanyCode(companyCode);
		rptA13.setId(id);
		
		if (!this.rptA13DAO.checkId(id)) {
			throw new ICISException("应收应付信息表的主键不能重复");
		}
		RptA13Bak bak = copyBakValue(rptA13);
		bak.setOprCode(userCode);
		bak.setOprDateTime(new Date());
		bak.setOpType(RptA13Bak.OPTYPE_ADD);
		this.rptA13DAO.saveBak(bak);
		this.rptA13DAO.save(rptA13);
	}
		
	/**
	 * A13信息的修改
	 * 
	 * @author zhangxr
	 * @param oldId
	 *            原A13表的主键
	 * @param rptA13
	 *            A13实体
	 * @param userCode 用户编号
	 */
	@Override
	public void modifyRptA13(RptA13Id oldId, RptA13 rptA13,String userCode) throws ICISException {
		RptA13Id id = rptA13.getId();
		if (!id.equals(oldId) 
				&& !this.rptA13DAO.checkId(id)) {
			throw new ICISException("应收应付信息表的主键不能重复");
		}
		RptA13 oldA13 = this.rptA13DAO.findRptA13ById(oldId);
		if(oldA13 == null){
			throw new ICISException("原始数据已经不存在");
		}
		//修改时记两条，一条删除，一条新增
		RptA13Bak bak = copyBakValue(oldA13);
		bak.setOprCode(userCode);
		bak.setOprDateTime(new Date());
		bak.setOpType(RptA13Bak.OPTYPE_DEL);
		rptA13DAO.saveBak(bak);
		
		RptA13Bak bak1 = copyBakValue(rptA13);
		bak1.setOprCode(userCode);
		bak1.setOprDateTime(new Date());
		bak1.setOpType(RptA13Bak.OPTYPE_ADD);
		rptA13DAO.saveBak(bak1);
		
		rptA13DAO.update(oldId, rptA13);
		
	}
	
	/**
	 * 根据主键，删除A13信息
	 * @author zhangxr
	 * @param id A13表主键
	 * @param userCode 用户编号
	 * */
	@Override
	public void deleteRptA13(RptA13Id id,String userCode)throws ICISException{
		RptA13 oldA13 = this.rptA13DAO.findRptA13ById(id);
		if(oldA13 == null){
			throw new ICISException("原始数据已经不存在");
		}
		RptA13Bak bak = copyBakValue(oldA13);
		bak.setOprCode(userCode);
		bak.setOprDateTime(new Date());
		bak.setOpType(RptA13Bak.OPTYPE_DEL);
		
        this.rptA13DAO.saveBak(bak);
		this.rptA13DAO.delete(id);
	}
	
	private RptA13Bak copyBakValue(RptA13 rptA13) {
		RptA13Bak bak = new RptA13Bak();
		bak.setCompanyCode(rptA13.getId().getCompanyCode());
		bak.setTradeDate(rptA13.getId().getTradeDate());
		bak.setArapType(rptA13.getId().getArapType());
		bak.setOppPar(rptA13.getId().getOppPar());
		bak.setMktCodeSipf(rptA13.getId().getMktCodeSipf());
		bak.setCurTypeSipf(rptA13.getId().getCurTypeSipf());
		bak.setArapBusiType(rptA13.getId().getArapBusiType());
		bak.setHarapDate(rptA13.getId().getHarapDate());
		bak.setArapReason(rptA13.getArapReason());
		bak.setArapAmt(rptA13.getArapAmt());
		bak.setArapDate(rptA13.getArapDate());
		bak.setSummary(rptA13.getSummary());
		bak.setBusiDate(rptA13.getId().getBusiDate());
		return bak;
	}
	/**
	 * 获得新的证券公司编码
	 * @author zhangxr
	 * */
	@Override
	public String getNewCompanyCode(String busiDate){
		return this.submitConfDAO.findCompanyCode(busiDate);
	}

}
